package com.study.rabbitmq.basic.defaultexchange;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class DefaultExchangeConsumerTest {
	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.20.68");
		factory.setPort(5672);
		factory.setUsername("leke");
		factory.setPassword("leke@@@");

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel(5);

		String queueName = "a.b.*";
		channel.queueDeclare(queueName,false,false,false,null);

//		//pull
//		while (true){
//			GetResponse response = channel.basicGet(queueName, true);
//			if(response!=null){
//				System.out.println(new String(response.getBody()));
//			}
//		}

		Consumer consumer = new DefaultConsumer(channel){
			@Override
			public void handleConsumeOk(String consumerTag) {
				super.handleConsumeOk(consumerTag);
			}

			@Override
			public void handleCancelOk(String consumerTag) {
				super.handleCancelOk(consumerTag);
			}

			@Override
			public void handleCancel(String consumerTag) throws IOException {
				super.handleCancel(consumerTag);
			}

			@Override
			public void handleRecoverOk(String consumerTag) {
				super.handleRecoverOk(consumerTag);
			}

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				System.out.println("consumer tag :" +consumerTag+"  "+new String(body));
				//channel.basicAck(envelope.getDeliveryTag(), false);

				channel.basicNack(envelope.getDeliveryTag(), false, false);
			}
		};
		//push  默认autoAck 为false
		String consumerTag = channel.basicConsume(queueName, consumer);

		//Cancel a consumer
		//channel.basicCancel(consumerTag);



	}
}
